const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

// 确保目录结构存在
const directories = [
  'src/modules/deployment/dto',
  'src/modules/deployment/entities',
  'src/modules/resources/dto',
  'src/modules/resources/entities',
  'src/modules/reports/dto',
  'src/modules/reports/entities',
  'src/modules/reports/controllers',
  'src/modules/reports/services',
  'src/modules/auth/dto',
  'src/modules/auth/entities',
  'src/modules/auth/guards',
  'src/modules/auth/strategies',
  'src/modules/metrics/dto',
  'src/modules/metrics/entities',
];

directories.forEach(dir => {
  const fullPath = path.join(__dirname, dir);
  if (!fs.existsSync(fullPath)) {
    fs.mkdirSync(fullPath, { recursive: true });
    console.log(`创建目录: ${fullPath}`);
  }
});

// 创建.env文件
const envContent = `# Database Configuration
DB_HOST=47.113.184.243
DB_PORT=3306
DB_USER=devops
DB_PWD=zTzAapS2EsmAzpFX
DB_DATABASE=devops
DB_SYNC=false
DB_LOGGING=true

# JWT Configuration
JWT_SECRET=devops-bi-secret
JWT_EXPIRATION=1h

# App Configuration
PORT=3000`;

const envPath = path.join(__dirname, '.env');
if (!fs.existsSync(envPath)) {
  fs.writeFileSync(envPath, envContent);
  console.log('创建.env文件');
} else {
  console.log('.env文件已存在，跳过创建');
}

// 安装依赖
console.log('安装依赖中...');
try {
  execSync('npm install', { stdio: 'inherit' });
  console.log('依赖安装完成');
  
  // 安装额外的类型定义
  execSync('npm install --save-dev @types/express-session @types/passport-jwt @types/passport-local', { stdio: 'inherit' });
  console.log('额外类型定义安装完成');
} catch (error) {
  console.error('依赖安装失败:', error.message);
}

console.log(`
======================================
项目初始化完成!

数据库配置:
- 主机: 47.113.184.243
- 端口: 3306
- 用户: devops
- 数据库: devops

运行开发服务器:
npm run start:dev

构建项目:
npm run build

======================================
`); 